<template>
    <div class="list" ref="wrapper">
        <div>
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="wrapper">
                    <div class="content">
                        <div class="button">{{this.city}}</div>
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="wrapper">
                    <div class="content"
                         v-for="(city, index) of hot"
                         :key="index"
                         @click="handleCityClick(city.name)"
                    >
                        <div class="button">{{city.name}}</div>
                    </div>
                </div>
            </div>
            <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
                <div class="title border-topbottom">{{key}}</div>
                <ul>
                    <li class="item border-bottom"
                        v-for="city of item"
                        :key="city.id"
                        @click="handleCityClick(city.name)"
                    >
                        {{city.name}}
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
import BScroll from 'better-scroll'
import {mapState, mapActions} from 'vuex'

export default {
  name: 'CityList',
  props: {
    cities: Object,
    hot: Array,
    letter: String
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    handleCityClick (cityName) {
      this.cityChange(cityName)
      this.$router.push('/')
    },
    ...mapActions(['cityChange'])
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  },
  watch: {
    letter () {
      if (this.letter) {
        const ele = this.$refs[this.letter][0]
        this.scroll.scrollToElement(ele)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.list
    position absolute
    top 1.58rem
    left 0
    right 0
    bottom 0
    overflow hidden
    .border-topbottom
        &:before
            color black
        &:after
            color black
    .border-bottom
        &:before
            border-color #ccc
    .title
        height .54rem
        line-height .54rem
        background-color #eee
        padding-left .3rem
    .wrapper
        overflow hidden
        padding .1rem .8rem .1rem .1rem
        .content
            float left
            width 25%
            .button
                margin .1rem .2rem .1rem .1rem
                padding .1rem
                text-align center
                border #ccc solid .02rem
    .item
        padding .2rem .3rem

</style>
